package com.example.baserecyclerviewadapterdemo.adapter;

import com.chad.library.adapter.base.BaseProviderMultiAdapter;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.baserecyclerviewadapterdemo.R;
import com.example.baserecyclerviewadapterdemo.entity.ProviderMultiEntity;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class ProviderMultiAdapter extends BaseProviderMultiAdapter<ProviderMultiEntity> {
    public ProviderMultiAdapter() {
        super();
        addItemProvider(new ImgItemProvider());
        addItemProvider(new TextImageProvider());
        addItemProvider(new TextItemProvider());
    }

    @Override
    protected int getItemType(@NotNull List<? extends ProviderMultiEntity> list, int position) {
        switch (position % 3) {
            case 0:
                return ProviderMultiEntity.IMG;
            case 1:
                return ProviderMultiEntity.TEXT;
            case 2:
                return ProviderMultiEntity.IMG_TEXT;
            default:
                break;
        }
        return 0;
    }

    //图片类型视图
    public static class ImgItemProvider extends BaseItemProvider<ProviderMultiEntity> {

        @Override
        public int getItemViewType() {
            return ProviderMultiEntity.IMG;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_image_view;
        }

        @Override
        public void convert(@NotNull BaseViewHolder baseViewHolder, ProviderMultiEntity providerMultiEntity) {
            switch (baseViewHolder.getLayoutPosition() % 2) {
                case 0:
                    baseViewHolder.setImageResource(R.id.iv,R.mipmap.animation_img1);
                    break;
                case 1:
                    baseViewHolder.setImageResource(R.id.iv,R.mipmap.animation_img3);
                    break;
            }
        }
    }

    //文本视图类型
    public class TextItemProvider extends BaseItemProvider<ProviderMultiEntity>{

        @Override
        public int getItemViewType() {
            return ProviderMultiEntity.TEXT;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_text_view;
        }

        @Override
        public void convert(@NotNull BaseViewHolder baseViewHolder, ProviderMultiEntity providerMultiEntity) {
            baseViewHolder.setText(R.id.tv,"CymChad content:" + baseViewHolder.getLayoutPosition());
        }
    }

    //文本、图片视图类型
    private class TextImageProvider extends BaseItemProvider<ProviderMultiEntity> {
        @Override
        public int getItemViewType() {
            return ProviderMultiEntity.IMG_TEXT;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_img_text_view;
        }

        @Override
        public void convert(@NotNull BaseViewHolder baseViewHolder, ProviderMultiEntity providerMultiEntity) {
            baseViewHolder.setImageResource(R.id.iv,R.mipmap.animation_img2);
            baseViewHolder.setText(R.id.tv,providerMultiEntity.getName());
        }
    }
}
